define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'channel/quotalist/index' + location.search,
                    add_url: 'channel/quotalist/add',
                    // edit_url: 'channel/quotalist/edit',
                    // del_url: 'channel/quotalist/del',
                    multi_url: 'channel/quotalist/multi',
                    import_url: 'channel/quotalist/import',
                    table: 'play',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'play_channel_quota_id',
                sortName: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                escape:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'play_channel_quota.id', title: __('Id')},
                        {field: 'id', title: '活动ID'},
                        {field: 'shop.id', title: '店铺ID'},
                        {field: 'shop.shop_name', title: '店铺名称', operate: 'LIKE', align: 'left',halign:"center"},
                        {field: 'shop_type', searchList: {"1":"美团","2":"饿了么","3":"京东"}, title: '活动平台', formatter: Table.api.formatter.status},
                        {field: 'back_money_text', title: '活动餐标',operate:false},
                        {field: 'num', title: '活动名额', formatter: Table.api.formatter.normal,operate:false, align: 'left',halign:"center"},
                        {field: 'scheme_no', searchList: {"1":"用餐反馈","2":"图文反馈","3":"无需反馈"}, title: '活动方案', formatter: Table.api.formatter.status},
                        {field: 'end_time_str', title: '活动日期', addclass:'datetimerange'},
                        {field: 'start_time_str', title: '活动时间',operate:false},
                        {field: 'play_channel_quota.quota', title: '已出单名额'},
                        {field: 'play_channel_quota.connector', title: '渠道业务员',searchList: Config.salesmanList},
                        {field: 'play_channel_quota.operate_status', searchList: {"1":"已处理","0":"未处理"}, title: '操作状态', formatter: Table.api.formatter.status},
                        {field: 'shop.address', title: '店铺地址', operate: 'LIKE', align: 'left',halign:"center"},
                        {field: 'create_dt', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'play_channel_quota.update_dt', title: __('Updatetime'),operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', 
                        title: __('Operate'), 
                        table: table, 
                        events: Table.api.events.operate, 
                        buttons: [
                        {
                            name: "edits",
                            title: "填写出单名额",
                            classname: "btn btn-xs btn-success btn-dialog",
                            icon: "fa",
                            text: "填写出单名额",
                            url: "channel/quotalist/edits",
                            refresh: true
                        },
                        ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'channel/quotalist/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop_name', title: __('Name'), align: 'left'},
                        {
                            field: 'delete_dt',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'channel/quotalist/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'channel/quotalist/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edits: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    //这里是表单提交处理成功后的回调函数，接收来自php的返回数据
                    Fast.api.close(data);//这里是重点
                    Toastr.success("成功");//这个可有可无
                }, function(data, ret){
                    Toastr.success("失败");
                });
            }
        }
    };
    return Controller;
});
